import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';



class ComplexScrollFixed extends StatelessWidget {
  const ComplexScrollFixed({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      debugShowCheckedModeBanner: false,
      home: ScrollPage(),
    );
  }
}


class ScrollPage extends StatefulWidget {
  const ScrollPage({super.key});

  @override
  State<ScrollPage> createState() => _ScrollPageState();
}

class _ScrollPageState extends State<ScrollPage> with TickerProviderStateMixin {
  ScrollController _scrollController = ScrollController();
  late AnimationController _controller;
  late Animation<double> _animation;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _controller = AnimationController(vsync: this, duration: const Duration(milliseconds: 300));

    _animation = Tween<double>(begin: 1.0, end: 0.0).animate(_controller)
    ..addListener(() {
        setState(() {
          
        });
      });

    // _controller.forward();

    _scrollController.addListener(_scroll);
  }

  void _scroll() {
    if (_scrollController.position.userScrollDirection == ScrollDirection.forward) {
      // print(111111);
      // print(_scrollController.offset);
      if (_scrollController.offset <= 0) {
        _controller.reverse();
      }
    } else {
      _controller.forward();
    }
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _controller.dispose();
    super.dispose();
    _scrollController.dispose();
  }


  Widget _renderBody() {
    return DefaultTabController(
      length: 3,
      child: NestedScrollView(
        controller: _scrollController,
        headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
          return <Widget>[
            // SliverAppBar(
            //   // title: const Text('吸顶标题'),
            //   pinned: false,
            //   floating: false,
            //   expandedHeight: 260.0,
            //   flexibleSpace: FlexibleSpaceBar(
            //     background: Image.network(
            //       "https://via.placeholder.com/350x150",
            //       fit: BoxFit.cover,
            //     ),
            //   ),
            // ),

            SliverToBoxAdapter(
              child: Container(
                width: double.infinity,
                height: 200,
                color: Colors.grey[300],
              ),
            ),
            SliverPersistentHeader(
              delegate: _SliverAppBarDelegate(const TabBar(
                labelColor: Colors.black87,
                unselectedLabelColor: Colors.grey,
                tabs: [
                  Tab(text: "标签一"),
                  Tab(text: "标签二"),
                  Tab(text: "标签三"),
                ],
              )),
              pinned: true,
            ),
          ];
        },
        body: const TabBarView(
          children: [
            Center(child: Text("第一个标签的内容")),
            Center(child: Text("第二个标签的内容")),
            Center(child: Text("第三个标签的内容"))
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          if (_controller.isCompleted) {
            _controller.reverse();
          } else {
            _controller.forward();
          }
        }
      ),
      appBar: AppBar(
        titleSpacing: 0.0,
        toolbarHeight: 100 - 50 * (1 - _animation.value),
        title: Container(
          width: double.infinity,
          height: 100 - 50 * (1 - _animation.value),
          color: Colors.grey,
          child: Stack(
            children: [
              const Row(
                children: [
                  Text('logo'),
                  Expanded(child: SizedBox()),
                  Icon(Icons.notifications)
                ],
              ),
              Positioned(
                bottom: 5,
                left: 50 * (1 - _animation.value),
                child: Container(
                  width: 360 - 100 * (1 - _animation.value),
                  height: 40,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(20),
                    color: Colors.blue[200],
                  ),
                )
              )
            ],
          ),
        ),
      ),
      
      body: _renderBody()
    );
  }
}



class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
  _SliverAppBarDelegate(this._tabBar);

  final TabBar _tabBar;

  @override
  double get minExtent => _tabBar.preferredSize.height;
  @override
  double get maxExtent => _tabBar.preferredSize.height;

  @override
  Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
    return Container(
      color: Colors.white,
      child: _tabBar,
    );
  }

  @override
  bool shouldRebuild(_SliverAppBarDelegate oldDelegate) {
    return false;
  }
}
